﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

namespace Jumpton.SFA.Server.BusinessRule.System
{
    public class GroupManager : BusinessLogic
    {
        /// <summary>
        /// 创建一个用户组
        /// </summary>
        /// <param name="code"></param>
        /// <param name="domainID"></param>
        /// <param name="createBy"></param>
        /// <returns></returns>
        public static decimal CreateGroup(string code, decimal domainID, string createBy)
        {
            USER_GROUPS group = new USER_GROUPS();
           

            group.CODE = code;
            group.DOMAINID = domainID;
            group.ACTIVE = true;
            group.LAST_UPDATER = createBy;
            group.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            try
            {
                group.Save();
                return group.ID;
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
                return decimal.MinValue;
            }
        }

        public DataTable LoadWithRecursion(Nullable<decimal> domainID, decimal groupID, int Level,int status)
        {
            string SQL = string.Format(@"SELECT UG.ID,
                                        UG.CODE,
                                        UG.DOMAIN_ID,
                                        D.SYS_NAME,
                                        UG.LAST_UPDATER,
                                        UG.LAST_UPDATE_TIME,
                                        UG.ACTIVE 
                                        FROM USER_GROUPS UG 
                                         INNER JOIN DOMAINS D ON D.ID = UG.DOMAIN_ID
                                        WHERE UG.DOMAIN_ID = {0}", domainID);
            if (status != 2)
            {
                SQL += " and UG.Active='" + status + "'";
            }

            DataTable groups = Jumpton.Data.SqlData.OpenSql(SQL).Tables[0];

            

            DataTable domains = new DomainManager().GetDomainsWithRecursion(domainID.Value);

            foreach (DataRow group in groups.Rows)
            {
                if (domains.Select("id=" + group["DOMAIN_ID"].ToString()).Length == 0)
                {
                    group.Delete();
                }
            }
            groups.AcceptChanges();
            return groups;
        }

        //public USER_GROUPS GetGroupByDomain(decimal domain)
        //{
        //    return (USER_GROUPS)ObjectRow.Load(typeof(USER_GROUPS), "this.DOMAINID='" + domain + "'");
        //}

        public USER_GROUPS Load(decimal groupID)
        {
            return (USER_GROUPS)ObjectRow.Load(typeof(USER_GROUPS), "this.ID = '" + groupID + "'");
        }

        public DataTable Load(string groupName)
        {
            return Jumpton.Data.SqlData.OpenSql("select * from USER_GROUPS where CODE like '%" + groupName + "%'").Tables[0];
        }

        public bool Save(int GroupID,String Group_Name, decimal DomainID,Boolean Active, String Rights,decimal LastUpdater)
        {
            int resultValue =Jumpton.Data.SqlData.ExecuteSP("SP_EDIT_USER_GROUP", GroupID, Group_Name, DomainID, LastUpdater, Active, Rights);
            return resultValue >= 0;
        }



    }
}
